package com.cskaoyan.service.wx.impl;

import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.wx.aftersale.WXAftersaleVo;
import com.cskaoyan.bean.wx.order.OrderDetailAfterSale;
import com.cskaoyan.bean.wx.order.WxOrderGoodsDetail;
import com.cskaoyan.controller.wx.order.OrderAfterSaleSubmitBo;
import com.cskaoyan.mapper.MarketAftersaleMapper;
import com.cskaoyan.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.mapper.wx.WxMarketOrderMapper;
import com.cskaoyan.service.wx.AfterSaleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author LiangRuiJiang
 * @since 2022/06/09  11:42
 */
@Service
public class AfterSaleServiceImpl implements AfterSaleService {

    @Autowired
    MarketOrderMapper marketOrderMapper;

    @Autowired
    WxMarketOrderMapper wxMarketOrderMapper;

    @Autowired
    MarketAftersaleMapper marketAftersaleMapper;

    @Autowired
    MarketOrderGoodsMapper marketOrderGoodsMapper;
    @Override
    public OrderDetailAfterSale queryOrderDetailAfterSale(Integer orderId) {
        OrderDetailAfterSale orderDetailAfterSale = new OrderDetailAfterSale();

        MarketAftersaleExample example = new MarketAftersaleExample();
        MarketAftersaleExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        criteria.andDeletedEqualTo(false);
        List<MarketAftersale> marketAftersales = marketAftersaleMapper.selectByExample(example);
        if(marketAftersales != null && marketAftersales.size() == 1){
            orderDetailAfterSale.setAftersale(marketAftersales.get(0));
        }
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderId);
        orderDetailAfterSale.setOrder(marketOrder);

        List<WxOrderGoodsDetail> wxOrderGoodsDetails = wxMarketOrderMapper.selectOrderGoodsDetail(orderId);
        orderDetailAfterSale.setOrderGoods(wxOrderGoodsDetails);
        return orderDetailAfterSale;
    }

    @Override
    @Transactional
    public void submitAfterSaleReply(OrderAfterSaleSubmitBo saleOrderDetail, Integer userId) {
        // 插入售后信息
        MarketAftersale aftersale = getMarketAftersale(saleOrderDetail);
        marketAftersaleMapper.insertSelective(aftersale);
        // 更新订单状态
        MarketOrder order = new MarketOrder();
        // 申请售后，订单待评价数置为0
        order.setComments((short)0);
        // 订单状态改为202
        order.setOrderStatus((short)202);
        order.setRefundAmount(saleOrderDetail.getAmount());
        //  退款方式
        order.setRefundType(saleOrderDetail.getTypeDesc());
        //  退款备注
        order.setRefundContent(saleOrderDetail.getReason());
        // 1表示已经申请售后
        order.setAftersaleStatus((short)1);
        order.setId(aftersale.getOrderId());
        marketOrderMapper.updateByPrimaryKeySelective(order);
    }



    /**
     * 获得售后信息
     * @param saleOrderDetail
     * @return
     */
    private MarketAftersale getMarketAftersale(OrderAfterSaleSubmitBo saleOrderDetail) {
        String idString = saleOrderDetail.getOrderId();
        Integer orderId = null;
        try {
            orderId = Integer.parseInt(idString);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        // 根据订单状态创建售后信息
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(orderId);

        MarketAftersale aftersale = new MarketAftersale();
        // 售后编号
        Long millis = System.currentTimeMillis();
        aftersale.setAftersaleSn(millis.toString());

        aftersale.setOrderId(marketOrder.getId());
        aftersale.setUserId(marketOrder.getUserId());
        aftersale.setType((short)saleOrderDetail.getType());

        aftersale.setAmount(saleOrderDetail.getAmount());
        aftersale.setPictures(saleOrderDetail.getPictures());
        // 售后状态 0是可申请，1是用户已申请，2是管理员审核通过，3是管理员退款成功，4是管理员审核拒绝，5是用户已取消'

        aftersale.setReason(saleOrderDetail.getReason());
        aftersale.setComment(saleOrderDetail.getTypeDesc());
        aftersale.setStatus((short)1);
        aftersale.setAddTime(new Date());
        return aftersale;
    }
}
